﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.InfoConfig;
using DevExpress.XtraEditors.Controls;
using MINERP.MODEL.FoundationModel;

namespace MINERP.InfoConfig
{
    public partial class FrmShopTechUserManager : FrmMDIChildFormTemplate
    {
        List<string> listUserName = new List<string>();
        ShopTechUserBLL stubll = new ShopTechUserBLL();
        ShopTechUserInfo _stuInfo = null;
        private static FrmShopTechUserManager instance = null;
        ShopBLL shopbll = new ShopBLL();
        public static FrmShopTechUserManager GetInstance
        {
            get 
            {
                if (instance == null)
                {
                    instance = new FrmShopTechUserManager();
                }
                return instance;
            }
        }
        private FrmShopTechUserManager()
        {
            InitializeComponent();
        }

        private void FrmShopTechUserManager_Load(object sender, EventArgs e)
        {
            LinkShopTechUser();
            LinkShop();
            LinkTech();
            LinkUser();

        }
        private void LinkShop()
        {

            foreach (var item in shopbll.GetShopList())
            {
                cmbShop.Properties.Items.Add(item.shopName);
            }
        }
        private void LinkTech()
        {
            TechBLL techbll = new TechBLL();
            foreach (var item in techbll.GetTechList())
            {
                cmbTech.Properties.Items.Add(item.TechName);
            }
        }
        private void LinkUser()
        {
            UserBLL userbll = new UserBLL();
            foreach (var item in userbll.GetUserList())
            {
                cmbUser.Properties.Items.Add(item.UserName);
            }
        }
        private void LinkShopTechUser()
        {

            gridControlShopTechUser.DataSource = stubll.GetShopTechUserInfoList();
        }

        private void FrmShopTechUserManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (var item in listUserName)
                {
                    if (stubll.IsExistSameShopTech(cmbShop.Text,cmbTech.Text,item))
                    {
                        //MessageBox.Show("该商户节点已存在","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        continue ;
                    }
                    if (DataCheck())
                    {                     
                        stubll.Add(cmbShop.Text, cmbTech.Text, item);
                                        
                    }
                }
                LinkShopTechUser();      
            }
            catch (Exception ex)
            {
             MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private bool DataCheck()
        {
            if (string.IsNullOrEmpty(cmbShop.Text))
            {
                MessageBox.Show("请选择商户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbTech.Text))
            {
                MessageBox.Show("请选择节点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbUser.Text))
            {
                MessageBox.Show("请选择操作员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void gridViewShopTechUser_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _stuInfo = (ShopTechUserInfo)gridViewShopTechUser.GetFocusedRow();
            cmbShop.Text = _stuInfo.ShopName;
            cmbTech.Text = _stuInfo.TechName;
            cmbUser.Text = _stuInfo.UserName;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //if (stubll.IsExistSameShopTech(_stuInfo.ShopName, cmbTech.Text))
            //{
            //    MessageBox.Show("该商户节点已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //if (DataCheck())
            //{
            //    try
            //    {
            //        stubll.Update(_stuInfo,cmbTech.Text,cmbUser.Text);
            //        LinkShopTechUser();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //}
        }

        private void btnDelegate_Click(object sender, EventArgs e)
        {
            if(DialogResult.OK ==MessageBox.Show("确定删除吗?","询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question))
            {
                try
                {
                    stubll.Delete(_stuInfo);
                    LinkShopTechUser();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void cmbUser_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            listUserName.Clear();
            foreach (CheckedListBoxItem item in cmbUser.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    listUserName.Add(item.Value.ToString());
                }
            }
        }

        private void textEditHelpCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode)
            {
                
                List<Shop_Info> shoplist = shopbll.GetShopInfoByHelpCode(textEditHelpCode.Text.Trim());
                if (shoplist.Count > 0)
                {
                    cmbShop.Properties.Items.Clear();
                    foreach (var item in shoplist)
                    {
                        cmbShop.Properties.Items.Add(item.ShopName);
                    }
                    cmbShop.SelectedIndex = 0;
                }


            }
        }
    }
}
